<?php

namespace backend\modules\common\controllers;

use backend\extensions\ApiBaseController;
use common\models\common\UploadModel;
use yii\web\UploadedFile;


class UploadController extends ApiBaseController {
    
    /**
     * 上传图片
     * * */
    public function actionPic() {
        $model = new UploadModel();
        if (\Yii::$app->request->isPost) {
            $model->file = UploadedFile::getInstanceByName('file');
//            $model->file = UploadedFile::getInstance($model, 'file');
            $paths = $model->upload();
            if ($paths) {
                return $this->jsonSuccess($paths);
            }
            return $this->jsonError('上传图片异常');
        }
    }

    /**
     * 上传图片——富文本文件上传固定返回格式
     * * */
    public function actionEditPic() {
        $model = new UploadModel();
        if (\Yii::$app->request->isPost) {
            $model->file = UploadedFile::getInstanceByName('file');
//            $model->file = UploadedFile::getInstance($model, 'file');
            $paths = $model->upload();
            if ($paths) {
                return [
                    "errno"=> 0, // 注意：值是数字，不能是字符串
                    "data"=> [
                        "url"=> $paths['path']
                    ]
                ];
            }
            return [
                "errno"=> 1, // 注意：值是数字，不能是字符串
                "data"=> [],
                "message"=> '上传图片异常'
            ];
        }
    }

}
